﻿<UserControl x:Class="TV_Organizer.FileScanner.FileScannerControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:metro="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
             xmlns:tvo="clr-namespace:TV_Organizer" xmlns:tvofs="clr-namespace:TV_Organizer.FileScanner"
             xmlns:tvop="clr-namespace:TV_Organizer.Properties" mc:Ignorable="d">
  <UserControl.Resources>
    <tvo:SeasonConverter x:Key="SeasonConverter" />

    <Style TargetType="CheckBox">
      <Setter Property="Width" Value="16" />
    </Style>

    <Style x:Key="SelectedShowComboBoxStyle" TargetType="ComboBox">
      <Setter Property="ItemTemplate">
        <Setter.Value>
          <DataTemplate>
            <TextBlock>
              <Run Text="{Binding Name, Mode=OneWay}" />
              <Run Text="{Binding Source, StringFormat=({0}), Mode=OneWay}" />
            </TextBlock>
          </DataTemplate>
        </Setter.Value>
      </Setter>
    </Style>

    <Style x:Key="NeedsMoveBorderStyle" TargetType="Border">
      <Style.Triggers>
        <DataTrigger Binding="{Binding NeedsMove}" Value="{x:Null}">
          <Setter Property="Background" Value="Transparent" />
        </DataTrigger>
        <DataTrigger Binding="{Binding NeedsMove}" Value="True">
          <!--  <Setter Property="Background" Value="#C0cb0000" />  -->
          <Setter Property="Background" Value="#C0FFA500" />
        </DataTrigger>
        <DataTrigger Binding="{Binding NeedsMove}" Value="False">
          <Setter Property="Background" Value="#A048cb00" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
    
    <Style x:Key="ProcessTextBlockStyle" TargetType="TextBlock">
      <Setter Property="Text" Value="RENAME" />
      <Style.Triggers>
        <DataTrigger Binding="{Binding Path=MoveTvShows, Source={x:Static tvop:Settings.Default}}" Value="True">
          <Setter Property="Text" Value="RENAME &amp; MOVE" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </UserControl.Resources>

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <!--  BUTTONS  -->
    <StackPanel Grid.Row="0" Orientation="Horizontal">

      <Button Name="ScanButton" Margin="0,0,5,5" Click="ScanButton_Click">Scan</Button>
      <Button Name="AnalyzeButton" Margin="0,0,5,5" Click="AnalyzeButton_Click">Analyze</Button>
      <Button Name="FetchEpisodeButton" Margin="0,0,5,5" Click="FetchEpisodeButton_Click">Fetch episode names</Button>
      <Button Name="ClearButton" Margin="0,0,5,5" Click="ClearButton_Click">Clear</Button>

      <Button Name="ProcessButton" Margin="0,0,5,5" Click="ProcessButton_Click">
        <TextBlock Style="{StaticResource ProcessTextBlockStyle}" />
      </Button>

      <TextBlock Margin="0,0,5,5" VerticalAlignment="Center" Text="Provider:" />
      <ComboBox Margin="0,0,5,5" ItemsSource="{Binding TVShowsServices, Source={x:Static tvo:ConnectorsManager.Instance}}" SelectedItem="{Binding Path=SelectedTVShowConnector, Source={x:Static tvop:Settings.Default}}" />
    </StackPanel>

    <ListView Name="FileScannerListView" Grid.Row="1" AllowDrop="True" DragOver="FileScannerListView_DragOver"
              Drop="FileScannerListView_Drop"
              ItemsSource="{Binding Path=Files, Source={x:Static tvofs:FileScanner.Instance}}"
              PreviewMouseDown="FileScannerListView_PreviewMouseDown" PreviewKeyDown="FileScannerListView_PreviewKeyDown"
              SourceUpdated="FileScannerListView_SourceUpdated">
      <ListView.Resources>
        <Style TargetType="ListViewItem">
          <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
      </ListView.Resources>
      <ListView.ContextMenu>
        <ContextMenu>
          <MenuItem Click="MenuEdit_Click" Header="Edit" />
        </ContextMenu>
      </ListView.ContextMenu>

      <ListView.View>
        <!--  GRIDVIEW  -->
        <GridView x:Name="FileScannerGridView" AllowsColumnReorder="True">
          <!-- COLUMN : Process checkbox -->
          <GridViewColumn>
            <GridViewColumn.Header>
              <CheckBox Name="CheckAllBoxesCheckBox" HorizontalAlignment="Center" VerticalAlignment="Center"
                        Checked="CheckAllBoxesCheckBox_Checked" Content="" ToolTip="Check to check all the files"
                        Unchecked="CheckAllBoxesCheckBox_Unchecked" />
            </GridViewColumn.Header>
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <CheckBox Name="ProcessFileCheckBox" Checked="ProcessFileCheckBox_Checked" IsChecked="{Binding ProcessFile}" ToolTip="Check to process this file." Unchecked="ProcessFileCheckBox_Unchecked" />
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>

          <!-- COLUMN : Filename -->
          <GridViewColumn>
            <GridViewColumn.Header>
              <TextBlock Text="OriginalFilename" />
            </GridViewColumn.Header>
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <TextBlock Text="{Binding Filename}" ToolTip="{Binding Path}" />
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>
          <!-- COLUMN : Extension -->
          <GridViewColumn DisplayMemberBinding="{Binding Path=Extension}">
            <GridViewColumn.Header>
              <TextBlock Text="Extension" />
            </GridViewColumn.Header>
          </GridViewColumn>

          <!-- COLUMN : Season -->
          <GridViewColumn DisplayMemberBinding="{Binding Path=Season, Converter={StaticResource SeasonConverter}}">
            <GridViewColumn.Header>
              <TextBlock Text="S" />
            </GridViewColumn.Header>
          </GridViewColumn>
          <!-- COLUMN : Episode -->
          <GridViewColumn DisplayMemberBinding="{Binding Path=Episode, Converter={StaticResource SeasonConverter}}">
            <GridViewColumn.Header>
              <TextBlock Text="E" />
            </GridViewColumn.Header>
          </GridViewColumn>

          <!-- COLUMN : Show -->
          <GridViewColumn>
            <GridViewColumn.Header>
              <TextBlock Text="Show" />
            </GridViewColumn.Header>
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <Grid>
                  <ComboBox Name="ShowSelectionComboBox" DisplayMemberPath="Name" ItemsSource="{Binding MatchingShows}" SelectedItem="{Binding SelectedShow}" SelectionChanged="ShowSelectionComboBox_SelectionChanged">
                    <ComboBox.Style>
                      <Style BasedOn="{StaticResource SelectedShowComboBoxStyle}" TargetType="ComboBox">
                        <Setter Property="Visibility" Value="Visible" />
                        <Style.Triggers>
                          <DataTrigger Binding="{Binding MatchingShows.Count}" Value="0">
                            <Setter Property="Visibility" Value="Collapsed" />
                          </DataTrigger>
                        </Style.Triggers>
                      </Style>
                    </ComboBox.Style>
                  </ComboBox>

                  <Button Name="EnterShowNameButton" Click="EnterShowNameButton_Click" Content="No match" ToolTip="Click to manualy enter the show name.">
                    <Button.Style>
                      <Style TargetType="Button">
                        <Setter Property="Visibility" Value="Collapsed" />
                        <Style.Triggers>
                          <DataTrigger Binding="{Binding MatchingShows.Count}" Value="0">
                            <Setter Property="Visibility" Value="Visible" />
                          </DataTrigger>
                        </Style.Triggers>
                      </Style>
                    </Button.Style>
                  </Button>
                </Grid>
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>

          <!-- COLUMN : Title -->
          <GridViewColumn DisplayMemberBinding="{Binding Path=EpisodeTitle}">
            <GridViewColumn.Header>
              <TextBlock Text="Title" />
            </GridViewColumn.Header>
          </GridViewColumn>

          <!-- COLUMN : Format -->
          <GridViewColumn DisplayMemberBinding="{Binding Path=Format}">
            <GridViewColumn.Header>
              <TextBlock Text="Format" />
            </GridViewColumn.Header>
          </GridViewColumn>
          <!-- COLUMN : Source -->
          <GridViewColumn DisplayMemberBinding="{Binding Path=Source}">
            <GridViewColumn.Header>
              <TextBlock Text="Source" />
            </GridViewColumn.Header>
          </GridViewColumn>
          <!-- COLUMN : Codec -->
          <GridViewColumn DisplayMemberBinding="{Binding Path=Codec}">
            <GridViewColumn.Header>
              <TextBlock Text="Codec" />
            </GridViewColumn.Header>
          </GridViewColumn>
          <!-- COLUMN : Team -->
          <GridViewColumn DisplayMemberBinding="{Binding Path=Team}">
            <GridViewColumn.Header>
              <TextBlock Text="Team" />
            </GridViewColumn.Header>
          </GridViewColumn>

          <!-- COLUMN : Rerip -->
          <GridViewColumn>
            <GridViewColumn.Header>
              <TextBlock Text="R" ToolTip="Checked if this is a rerip version" />
            </GridViewColumn.Header>
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <CheckBox IsChecked="{Binding IsRerip}" />
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>
          <!-- COLUMN : Internal -->
          <GridViewColumn>
            <GridViewColumn.Header>
              <TextBlock Text="I" ToolTip="Checked if this is an internal version" />
            </GridViewColumn.Header>
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <CheckBox IsChecked="{Binding IsInternal}" />
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>
          <!-- COLUMN : Real -->
          <GridViewColumn>
            <GridViewColumn.Header>
              <TextBlock Text="R" ToolTip="Checked if this is a real version" />
            </GridViewColumn.Header>
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <CheckBox IsChecked="{Binding IsReal}" />
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>
          <!-- COLUMN : Proper -->
          <GridViewColumn>
            <GridViewColumn.Header>
              <TextBlock Text="P" ToolTip="Checked if this is a proper version" />
            </GridViewColumn.Header>
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <CheckBox IsChecked="{Binding IsProper}" />
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>
          <!-- COLUMN : Repack -->
          <GridViewColumn>
            <GridViewColumn.Header>
              <TextBlock Text="R" ToolTip="Checked if this is a repack version" />
            </GridViewColumn.Header>
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <CheckBox IsChecked="{Binding IsRepack}" />
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>
          <!-- COLUMN : Sample -->
          <GridViewColumn>
            <GridViewColumn.Header>
              <TextBlock Text="S" ToolTip="Checked if this is a sample" />
            </GridViewColumn.Header>
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <CheckBox IsChecked="{Binding IsSample}" />
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>

          <!-- COLUMN : Destination file -->
          <GridViewColumn>
            <GridViewColumn.Header>
              <TextBlock Text="Destination file" />
            </GridViewColumn.Header>
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <Border Style="{StaticResource NeedsMoveBorderStyle}">
                  <TextBlock Text="{Binding Path=TargetFilename}" />
                </Border>
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>
          <!-- COLUMN : Destination folder -->
          <GridViewColumn DisplayMemberBinding="{Binding Path=TargetFolder}">
            <GridViewColumn.Header>
              <TextBlock Text="Destination folder" />
            </GridViewColumn.Header>
          </GridViewColumn>
        </GridView>
      </ListView.View>
    </ListView>

  </Grid>
</UserControl>
